package com.rpay.service.feedback.impl;

import com.rpay.common.dto.feedback.FeedbackDTO;
import com.rpay.common.dto.feedback.FeedbackQuertDTO;
import com.rpay.dao.feedback.FeedbackDao;
import com.rpay.model.feedback.FeedbackDO;
import com.rpay.model.feedback.FeedbackQueryDO;
import com.rpay.service.feedback.IFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FeedbackServiceImpl implements IFeedbackService {
    @Autowired
    private FeedbackDao feedbackDao;

    @Override
    public boolean saveFeedback(FeedbackDTO feedbackDTO) {
        FeedbackDO feedbackDO = convertDTOToDO(feedbackDTO);
        feedbackDao.insert(feedbackDO);
        return true;
    }

    @Override
    public FeedbackQuertDTO list(FeedbackQuertDTO feedbackQuertDTO, Integer page, Integer pageSize, String startTime, String endTime) {
        FeedbackQueryDO feedbackQueryDO = convertQueryDTOToDO(feedbackQuertDTO);
        List<FeedbackDO> feedbackDOList = feedbackDao.list(feedbackQueryDO);
        List<FeedbackDTO> list = new ArrayList<>();
        if (feedbackDOList != null && feedbackDOList.size() > 0) {
            for (FeedbackDO feedbackDO : feedbackDOList) {
                FeedbackDTO feedbackDTO = convertDOToDTO(feedbackDO);
                list.add(feedbackDTO);
            }
        }
        Integer count = feedbackDao.selectRow(feedbackQueryDO);
        FeedbackQuertDTO data = convertQueryDOToDTO(list, page, pageSize, count, startTime, endTime);
        return data;
    }

    @Override
    public boolean patch(FeedbackDTO feedbackDTO) {
        FeedbackDO feedbackDO = convertDTOToDO(feedbackDTO);
        feedbackDao.patch(feedbackDO);
        return true;
    }

    @Override
    public boolean delete(Integer id) {
        feedbackDao.delete(id);
        return true;
    }

    private FeedbackQuertDTO convertQueryDOToDTO(List list, Integer page, Integer pageSize, Integer count, String startTime, String endTime) {
        FeedbackQuertDTO feedbackQuertDTO = new FeedbackQuertDTO();
        feedbackQuertDTO.setPage(page);
        feedbackQuertDTO.setPageSize(pageSize);
        feedbackQuertDTO.setList(list);
        feedbackQuertDTO.setTotalRow(count);
        feedbackQuertDTO.setStartTime(startTime);
        feedbackQuertDTO.setEndTime(endTime);
        return feedbackQuertDTO;
    }

    private FeedbackQueryDO convertQueryDTOToDO(FeedbackQuertDTO feedbackQuertDTO) {
        FeedbackQueryDO feedbackQueryDO = null;
        if (feedbackQuertDTO != null) {
            feedbackQueryDO = new FeedbackQueryDO();
            feedbackQueryDO.setPage(feedbackQuertDTO.getPage());
            feedbackQueryDO.setPageSize(feedbackQuertDTO.getPageSize());
            feedbackQueryDO.setStartTime(feedbackQuertDTO.getStartTime());
            feedbackQueryDO.setEndTime(feedbackQuertDTO.getEndTime());
        }
        return feedbackQueryDO;
    }

    private FeedbackDTO convertDOToDTO(FeedbackDO feedbackDO) {
        FeedbackDTO feedbackDTO = null;
        if (feedbackDO != null) {
            feedbackDTO = new FeedbackDTO();
            feedbackDTO.setId(feedbackDO.getId());
            feedbackDTO.setFeedbackTitle(feedbackDO.getFeedbackTitle());
            feedbackDTO.setFeedbackDesc(feedbackDO.getFeedbackDesc());
            feedbackDTO.setUsername(feedbackDO.getUsername());
            feedbackDTO.setPhone(feedbackDO.getPhone());
            feedbackDTO.setEmail(feedbackDO.getEmail());
            feedbackDTO.setAddress(feedbackDO.getAddress());
            feedbackDTO.setIsRead(feedbackDO.getIsRead());
            feedbackDTO.setCreateTime(feedbackDO.getCreateTime());
        }
        return feedbackDTO;
    }

    private FeedbackDO convertDTOToDO(FeedbackDTO feedbackDTO) {
        FeedbackDO feedbackDO = null;
        if (feedbackDTO != null) {
            feedbackDO = new FeedbackDO();
            feedbackDO.setId(feedbackDTO.getId());
            feedbackDO.setIsRead(feedbackDTO.getIsRead());
            feedbackDO.setFeedbackTitle(feedbackDTO.getFeedbackTitle());
            feedbackDO.setFeedbackDesc(feedbackDTO.getFeedbackDesc());
            feedbackDO.setUsername(feedbackDTO.getUsername());
            feedbackDO.setPhone(feedbackDTO.getPhone());
            feedbackDO.setEmail(feedbackDTO.getEmail());
            feedbackDO.setAddress(feedbackDTO.getAddress());
        }
        return feedbackDO;
    }
}
